using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Opc.Ua;
using OpcuaClient.Model;

namespace OpcuaClient.View
{
    public partial class Form_Collection : Form
    {
        public bool Status { get; set; }
        public Collection Collection { get; set; }
        public Form_Collection()
        {
            InitializeComponent();
            Collection = new Collection();
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            Status = false;
            Close();
        }

        public void InitCollection(NodeId node)
        {
            string[] values = node.Identifier.ToString().Split('/');
            if (values.Length < 2)
            {
                values = node.Identifier.ToString().Split('.');
            }
            tbKey.Text = values.Last();
            tbName.Text = values.Last();
            cbDataType.Text = node.IdType.ToString();
            tbAddress.Text = node.ToString();
            Collection.Node = node;
        }

        public void InitCollection(Collection collection)
        {
            if (collection == null) return;

            tbKey.Text = collection.Key;
            tbName.Text = collection.Name;
            tbAddress.Text = collection.Address;
            tbTopic.Text = collection.Topic;
            cbDataType.Text = collection.DataType;
            cbReadType.Text = collection.ReadType;
            tbFrequency.Text = collection.Frequency.ToString();
            cbValueType.Text = collection.ValueType;
            tbValue.Text = collection.Value.ToString();
            cbWrightType.Text = collection.WriteType;
            tbPrefix.Text = collection.Prefix;
            tbPrecondition.Text = collection.Precondition;

            Collection = collection;
        }

        public void InitFixed()
        {
            cbValueType.Text = "Fixed";
            tbFrequency.Text = "0";
            tbAddress.Text = "";
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            if (Collection == null)
            {
                Collection = new Collection();
            }
            if (string.IsNullOrWhiteSpace(tbKey.Text) || string.IsNullOrWhiteSpace(tbKey.Text)
                || string.IsNullOrWhiteSpace(tbFrequency.Text) || string.IsNullOrWhiteSpace(tbAddress.Text)
                || string.IsNullOrWhiteSpace(cbDataType.Text) || string.IsNullOrWhiteSpace(cbReadType.Text)
                )
            {

                MessageBox.Show(this, "信息配置不完整，请检查!", "错误提示！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Collection.Key = tbKey.Text;
            Collection.Name = tbName.Text;
            Collection.Address = tbAddress.Text;
            Collection.Topic = tbTopic.Text;
            Collection.DataType = cbDataType.Text;
            Collection.ReadType = cbReadType.Text;
            Collection.Frequency = int.Parse(tbFrequency.Text);
            Collection.ValueType = cbValueType.Text;
            Collection.Value = tbValue.Text;
            Collection.WriteType = cbWrightType.Text;
            Collection.Prefix = tbPrefix.Text;
            Collection.Precondition = tbPrecondition.Text;

            Status = true;
            Close();
        }

        public void Config(Collection collection = null)
        {
            if (collection != null)
            {
                Collection = collection;
            }
            if (Collection == null)
            {
                Collection = new Collection();
                return;
            }

            tbKey.Text = Collection.Key;
            tbName.Text = Collection.Name;
            tbAddress.Text = Collection.Address;
            tbTopic.Text = Collection.Topic;
            cbDataType.Text = Collection.DataType;
            cbReadType.Text = Collection.ReadType;
            tbFrequency.Text = Collection.Frequency.ToString();
            cbValueType.Text = Collection.ValueType;
            tbValue.Text = Collection.Value.ToString();
            cbWrightType.Text = Collection.WriteType;
            tbPrefix.Text = Collection.Prefix;
            tbPrecondition.Text = Collection.Precondition;
            Status = true;
        }

        private void Form_Collection_Load(object sender, EventArgs e)
        {

        }
    }
}
